<?php

namespace App\Models;

use Validator;
use Illuminate\Validation\Rule;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Article extends Model
{
    use SoftDeletes;
    protected $fillable = ['title', 'tags', 'member_id', 'cate_id', 'is_top', 'status', 'desc', 'content'];
    protected $dateFormat = 'U';

    //关联会员
    public function members()
    {
        return $this->belongsTo('App\\Models\\Member', 'member_id', 'id');
    }

    //关联栏目
    public function cates()
    {
        return $this->belongsTo('App\\Models\\Cate', 'cate_id', 'id');
    }

    //关联评论
    public function comments()
    {
        return $this->hasMany('App\\Models\\Comment', 'article_id', 'id');
    }

    //文章添加
    public function add($data)
    {
        $rule = [
            'title' => 'bail|required|unique:articles',
            'tags' => 'required',
            'member_id' => 'required',
            'cate_id' => 'required',
            'desc' => 'required',
            'content' => 'required'
        ];

        $msg = [
            'title.required' => '标题不能为空',
            'title.unique' => '此标题已存在',
            'tags.required' => '标签不能为空',
            'member_id.required' => '作者必须选择',
            'cate_id.required' => '所属栏目必须选择',
            'desc.required' => '文章概要必须填写',
            'content.required' => '文章内容必须填写'
        ];
        $validator = Validator::make($data, $rule, $msg);
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        $result = $this->create($data);
        if ($result) {
            return 1;
        }else {
            return '文章添加失败';
        }
    }

    //编辑
    public function edit($data)
    {
        $rule = [
            'title' => [
                'bail',
                'required',
                Rule::unique('articles')->ignore($data['id'])
            ],
            'tags' => 'required',
            'member_id' => 'required',
            'cate_id' => 'required',
            'desc' => 'required',
            'content' => 'required'
        ];

        $msg = [
            'title.required' => '文章标题不能为空',
            'title.unique' => '文章标题已存在',
            'tags.required' => '标签不能为空',
            'member_id.required' => '作者必须选择',
            'cate_id.required' => '栏目必须选择',
            'desc.required' => '概要必须填写',
            'content.required' => '内容必须填写'
        ];
        $validator = Validator::make($data, $rule, $msg);
        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        $articleInfo = $this->find($data['id']);
        $articleInfo->title = $data['title'];
        $articleInfo->tags = $data['tags'];
        $articleInfo->member_id = $data['member_id'];
        $articleInfo->cate_id = $data['cate_id'];
        $articleInfo->desc = $data['desc'];
        $articleInfo->content = $data['content'];
        $result = $articleInfo->save();
        if ($result) {
            return 1;
        }else {
            return '文章修改失败';
        }
    }
}
